var rowTpl = "<option id=\"0\">  </option>";
var RegionHelper = {
    provinceObj: null,
    cityList: null,
    regionId: 0,
    checkDone: null,
    curProvinceId:0,
    curCityId:0,
    initProvince: function (provinceObj, provinceId) {
        RegionHelper.provinceObj = provinceObj;
        provinceObj.find('option').remove();
        var initRow = $(rowTpl).clone();
        initRow.text("省/直辖市");
        provinceObj.append(initRow);
        provinceObj.change(function(){
            RegionHelper.curProvinceId = $(this).val();
            RegionHelper.initCity($('#cityList'), RegionHelper.curProvinceId);
        });
        $('#cityList').change(function(){
            RegionHelper.curCityId = $(this).val();
            RegionHelper.initRegion($('#regionList'), RegionHelper.curProvinceId, RegionHelper.curCityId);
        });
        $('#regionList').change(function(){
            RegionHelper.regionId = $(this).val();
            RegionHelper.checkDone(RegionHelper.curProvinceId , RegionHelper.curCityId , RegionHelper.regionId);
        });

        $.each(regions, function (key, item) {
            var newRow = $(rowTpl).clone();
            if (item.id == provinceId) {
                newRow.attr('selected','');
            }
            newRow.attr('value', item.id);
            newRow.text(item.name);
            provinceObj.append(newRow);
        });
    },

    initCity: function (cityObjId, provinceId, selId) {
        if (cityObjId.attr('parentId') == provinceId) {
            return;
        }
        cityObjId.find('option').remove();
        var initRow = $(rowTpl).clone();
        initRow.text("城市");
        cityObjId.append(initRow);
        $.each(regions, function (i, item) {
            if (item.id == provinceId) {
                $('#cityList').attr('parentId', provinceId);
                $.each(item.subList, function (key, cityItem) {
                    var newRow = $(rowTpl).clone();
                    if (cityItem.id == selId) {
                        newRow.attr('selected','');
                    }
                    newRow.attr('value', cityItem.id);
                    newRow.text(cityItem.name);
                    $('#cityList').append(newRow);
                });
                return false;
            }
        });
    },

    initRegion: function (regionObj, provinceId, cityId, selId) {
        if (regionObj.attr('parentId') == cityId) {
            console.log("save ParentId");
            return;
        }
        regionObj.find('option').remove();
        var initRow = $(rowTpl).clone();
        initRow.text("县区");
        regionObj.append(initRow);
        regionObj.attr('parentId', cityId);
        console.log("enter initRegion");
        $.each(regions, function (i, item) {
            if (item.id == provinceId) {
                $.each(item.subList, function (key, city) {
                    if (city.id == cityId) {
                        $.each(city.subList, function (key, regionItem) {
                            var newRow = $(rowTpl).clone();
                            newRow.attr('value', regionItem.id);
                            if (regionItem.id == selId) {
                                newRow.attr('selected','');
                            }
                            newRow.text(regionItem.name);
                            regionObj.append(newRow);
                        });
                        return false;
                    }
                });
                return false;
            }
        });
    },
    getRegionName: function (regionId) {
        var regionName = '';
        var provinceName;
        var cityName;
        $.each(regions, function (key, province) {
            if (province.id == regionId) {
                return false;
            } else {
                $.each(province.subList, function (key, city) {
                    if (city.id == regionId) {
                        return false;
                    } else {
                        $.each(city.subList, function (key, item) {
                            if (item.id == regionId) {
                                provinceName = province.name;
                                cityName = city.name;
                                regionName += item.name;
                                return false;
                            }
                        });
                    }
                });
            }
        });
        return provinceName + cityName + regionName;
    },

    InitHometomeArea: function (regionId) {
        if (regionId && regionId > 0) {
            $.each(regions, function (key, province) {
                if (province.id == regionId) {
                    RegionHelper.initProvince($('#provinceList'), regionId);
                    return;
                } else {
                    $.each(province.subList, function (key, city) {
                        if (city.id == regionId) {
                            RegionHelper.initCity($('#regionList'), province.id, regionId);
                            return;
                        } else {
                            $.each(city.subList, function (key, item) {
                                if (item.id == regionId) {
                                    RegionHelper.initProvince($('#provinceList'), province.id);
                                    RegionHelper.initCity($('#regionList'), province.id, city.id);
                                    RegionHelper.initRegion($('#regionList'), province.id, city.id, regionId);
                                    return;
                                }
                            });
                        }
                    });
                }
            });
        } else {
            RegionHelper.initProvince($('#provinceList'), 0);
        }
    }
};
